package com.ruoyi.web.controller.tool.designpattern.creationalPatterns.abstractFactory;

/**
 * 抽像工厂模式
 *
 * @author Hack2003
 * @date 2024/3/20 14:34
 **/

// 抽象产品
interface Product {
    void doSomething();
}

// 具体产品 A
class ConcreteProductA implements Product {
    @Override
    public void doSomething() {
        System.out.println("Product A is doing something.");
    }
}

// 具体产品 B
class ConcreteProductB implements Product {
    @Override
    public void doSomething() {
        System.out.println("Product B is doing something.");
    }
}

// 抽象工厂
interface Factory {
    Product createProduct();
}

// 具体工厂 A
class ConcreteFactoryA implements Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProductA();
    }
}

// 具体工厂 B
class ConcreteFactoryB implements Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProductB();
    }
}

/**
 * 通过工厂模式，客户端代码只需要与抽象工厂和抽象产品接口交互，
 * 而不需要直接依赖具体产品类，从而实现了解耦
 */
public class Test {
    public static void main(String[] args) {
        Factory factoryA = new ConcreteFactoryA();
        Product productA = factoryA.createProduct();
        productA.doSomething();

        Factory factoryB = new ConcreteFactoryB();
        Product productB = factoryB.createProduct();
        productB.doSomething();
    }
}
